package com.realworld.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.realworld.common.R;
import com.realworld.mapper.ArticleMapper;
import com.realworld.mapper.UserInfoMapper;
import com.realworld.model.dto.ArticleReq;
import com.realworld.model.pojo.Article;
import com.realworld.model.pojo.UserInfo;
import com.realworld.service.ArticleService;
import org.springframework.stereotype.Service;
import sun.text.resources.FormatData;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class ArticleImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public List<Article> getArticleList(Article article) {
        if (article.getCount() == null){
            article.setCount(20);
        }
        List<Article> articleList = articleMapper.getArticleList(article);
        if (article.getUid()!=null){
            articleList.forEach(article1 -> {
                UserInfo userInfo = userInfoMapper.selectUserById(article1.getUid());
                article1.setUserInfo(userInfo);
            });
        }
        return articleList;
    }

    @Override
    public List<Article> getArticleInfoList(Integer uid) {
        return articleMapper.getArticleInfoList(uid);
    }

    @Override
    public Article getArticleInfo(Integer id) {
        return articleMapper.getArticleInfo(id);
    }

    @Override
    public R addArticle(ArticleReq articleReq, String uid) throws ParseException {
        HashMap<String,String> map = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(date);
        Date parse = formatter.parse(format);
        Article article = new Article();
        BeanUtil.copyProperties(articleReq,article);
        article.setCreateTime(parse);
        map.put("body", articleReq.getBody());
        map.put("description", articleReq.getDescription());
        String json = JSON.toJSONString(map);
        article.setContext(json);
        article.setUid(Integer.valueOf(uid));
        articleMapper.addArticle(article);
        return R.ok(article,4001);
    }

    @Override
    public R updateArticle(ArticleReq articleReq, String uid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("body", articleReq.getBody());
        map.put("description", articleReq.getDescription());
        Article article = new Article();
        BeanUtil.copyProperties(articleReq,article);
        articleMapper.updateArticle(article);
        return R.ok(article,4001);
    }

    @Override
    public R deleteArticle(String uid,Integer artId) {
        articleMapper.deleteArticle(uid,artId);
        return R.ok(4001,"delete success!");
    }
}
